// pages/rooms/rooms.ts
import { axios } from "../../utils/request"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    option1: [
      { text: "1-2人", value: 1 },
      { text: "3人", value: 2 },
      { text: "3+人", value: 3 },
    ],
    option2: [
      { text: "所有价格", value: 0 },
      { text: "低价优先", value: 1 },
      { text: "高价优先", value: 2 },
    ],
    option3: [
      { text: "默认", value: "" },
      { text: "空调", value: "air" },
      { text: "洗衣机", value: "washer" },
      { text: "无线网络", value: "wifi" },
      { text: "投影仪", value: "pojector" },
    ],
    condition: {
      service: "wifi",
      bedroom: 1,
      price: 0,
    },
    keyword: "",
    isEmpt: false,
    initData: [] as Room[],
    userStart: [],
  },
  async init() {
    let isLogin = wx.getStorageSync("isLogin")
    if (isLogin) {
      let data = (await axios("/room")) as Response
      const startRes = (await axios("/start")) as Response
      let initData = data.data.map((item) => {
        startRes.data.forEach((v: any) => {
          if (item.id == v.roomId) {
            item.isLike = true
          }
        })
        return item
      })
      this.setData({
        initData,
        isEmpt: false,
      })
    } else {
      let roomData = (await axios("/room")) as Response
      this.setData({
        initData: roomData.data,
        isEmpt: false,
      })
    }
  },
  //收藏
  async start(e: any) {
    const isLogin = wx.getStorageSync("isLogin")
    if (isLogin) {
      let id = e.currentTarget.dataset.id
      ;(await axios("/start", "POST", {
        roomId: id,
      })) as Response
      let index = e.currentTarget.dataset.index
      let initData = this.data.initData
      initData[index].isLike = true
      this.setData({
        initData,
      })
    } else {
      wx.showToast({
        title: "请先登录",
        icon: "none",
      })
    }
  },
  //取消收藏
  async cancelStart(e: any) {
    let id = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index
    let initData = this.data.initData
    ;(await axios("/start/cancel", "POST", {
      roomId: id,
    })) as Response
    initData[index].isLike = false
    this.setData({
      initData,
    })
  },
  async orderChange(e: any) {
    wx.showLoading({ title: "加载中", mask: true })
    const name = e.currentTarget.dataset.name
    const condition = this.data.condition as any
    condition[name] = e.detail
    let data = (await axios("/room/condition", "GET", {
      ...condition,
    })) as Response

    const startRes = (await axios("/start")) as Response
    data.data.map((item) => {
      startRes.data.forEach((v: any) => {
        if (item.id == v.roomId) {
          item.isLike = true
        }
      })
      return item
    })
    if (data.data.length) {
      this.setData({
        initData: data.data,
        condition,
        isEmpt: false,
      })
    } else {
      this.setData({
        initData: data.data,
        condition,
        isEmpt: true,
      })
    }
    setTimeout(function () {
      wx.hideLoading()
    }, 200)
  },
  toDetail(e: any) {
    let item = e.currentTarget.dataset.index
    wx.navigateTo({
      url: "/pages/roomDetail/roomDetail",
      success: (res) => {
        // 通过 eventChannel 向被打开页面传送数据
        res.eventChannel.emit("context", this.data.initData[item])
      },
    })
  },
  onChange(e: any) {
    if (e.detail == "") {
      this.init()
    }
    this.setData({
      keyword: e.detail,
    })
  },
  async onSearch() {
    wx.showLoading({ title: "加载中", mask: true })
    let data = (await axios("/room/keyword", "GET", {
      keyword: this.data.keyword,
    })) as Response
    if (data.data.length) {
      this.setData({
        initData: data.data,
        isEmpt: false,
      })
    } else {
      this.setData({
        initData: [],
        isEmpt: true,
      })
    }
    setTimeout(function () {
      wx.hideLoading()
    }, 200)
  },
  async onShow() {
    wx.showLoading({ title: "加载中", mask: true })
    this.init()
    setTimeout(function () {
      wx.hideLoading()
    }, 200)
  },
})
